package com.bos.data.repositories.impl;

import com.bos.data.repositories.ProjectRepository;
import com.bos.common.PageData;
import com.google.common.base.Strings;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@Transactional
public class ProjectRepositoryImpl extends BaseRepository implements ProjectRepository {
    public PageData findWithTerm(String term, List<String> shuigou, List<String> wtype, List<String> pteam, List<String> pleader, int pageSize, int pageIndex) {
        StringBuffer sb = new StringBuffer();
        Map map = new HashMap<>();
        sb.append("select * from bos_project where 1=1");
        if (!Strings.isNullOrEmpty(term) && !term.trim().isEmpty()) {
            if (term.matches("[0-9]*")) {
                sb.append(" and ( id like :term or wType like :term ");
            } else {
                sb.append(" and (  wType like :term ");

            }
            sb.append("or  wName like :term " +
                    "or  sTime like :term " +
                    "or  eTime like :term " +
                    "or  pdetail like :term " +
                    "or  leader like :term " +
                    "or  groups like :term " +
                    "or  checker like :term " +
                    "or  kehuer like :term " +
                    "or  others1 like :term " +
                    "or  others2 like :term " +
                    "or  remarks like :term ) ");
            map.put("term", "%" + term + "%");
        }
        //shuigou
        if (shuigou!=null && shuigou.size() != 0) {
            String permit = "";
            for (int a = 0; a < shuigou.size(); a++) {
                if (a == 0) {
                    permit = permit + "'" + shuigou.get(a) + "'";
                } else {
                    permit = permit + ", " + "'" + shuigou.get(a) + "'";
                }
            }
            permit = "(" + permit + ")";
            sb.append(" and progess in " + permit);
        }

        //wtype
        if (wtype!=null && wtype.size() != 0) {
            String permit = "";
            for (int a = 0; a < wtype.size(); a++) {
                if (a == 0) {
                    permit = permit + "'" + wtype.get(a) + "'";
                } else {
                    permit = permit + ", " + "'" + wtype.get(a) + "'";
                }
            }
            permit = "(" + permit + ")";
            sb.append(" and wtype in " + permit);
        }

        //pteam
        if (pteam!=null && pteam.size() != 0) {
            String permit = "";
            for (int a = 0; a < pteam.size(); a++) {
                if (a == 0) {
                    permit = permit + "'" + pteam.get(a) + "'";
                } else {
                    permit = permit + ", " + "'" + pteam.get(a) + "'";
                }
            }
            permit = "(" + permit + ")";
            sb.append(" and groups in " + permit);
        }

        //pleader
        if (pleader!=null && pleader.size() != 0) {
            String permit = "";
            for (int a = 0; a < pleader.size(); a++) {
                if (a == 0) {
                    permit = permit + "'" + pleader.get(a) + "'";
                } else {
                    permit = permit + ", " + "'" + pleader.get(a) + "'";
                }
            }
            permit = "(" + permit + ")";
            sb.append(" and leader in " + permit);
        }
        List l= Arrays.asList("update_time");
        return this.getPage(sb.toString(),map,pageSize,pageIndex,l,true);
    }
}
